﻿Imports Oracle.DataAccess.Client
Imports lbGlobalcom
Public Class fListaMovimi
    Dim daTipoDoc As lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
    Dim daSedes As lbGlobalcom.dsGlobalTableAdapters.GLO_SEDESTableAdapter
    Private lTipoMovimi As String = ""
    Private lTipoDoc As String = ""
    Private lNumeroDoc As Integer = 0
    Private lSubClase As String = "VEN"
    Private lTercero As String = ""
    Private lSucursal As Integer = -1
    Private lSaldo As Integer = 1
    Public ReadOnly Property mTipoDoc() As String
        Get
            Return lTipoDoc
        End Get
    End Property
    Public ReadOnly Property mNumeroDoc() As Integer
        Get
            Return lNumeroDoc
        End Get
    End Property
    Public Property TipoMovimi() As String
        Get
            Return lTipoMovimi
        End Get
        Set(ByVal value As String)
            lTipoMovimi = value
        End Set
    End Property

    Public Property SubClase() As String
        Get
            Return lSubClase
        End Get
        Set(ByVal value As String)
            lSubClase = value
        End Set
    End Property

    Public Property mTercero() As String
        Get
            Return lTercero
        End Get
        Set(ByVal value As String)
            lTercero = value
        End Set
    End Property
    Public Property mSucursal() As Integer
        Get
            Return lSucursal
        End Get
        Set(ByVal value As Integer)
            lSucursal = value
        End Set
    End Property
    Public Property mSaldo() As Integer
        Get
            Return lSaldo
        End Get
        Set(ByVal value As Integer)
            lSaldo = value
        End Set
    End Property



    Private Sub btnConsulta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsulta.Click
        Dim conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = conn.CreateCommand
        Dim qFrom As String = "FROM MOV_MOVIMIENTO, GLO_TIPODOCUMENTO"
        Dim qWhere As String = "WHERE MOV_MOVIMIENTO.FECHADOCUMENTO BETWEEN :PDESDEF AND :PHASTAF AND GLO_TIPODOCUMENTO.DOCES = :PDOCES AND MOV_MOVIMIENTO.IDDOCUMENTO = GLO_TIPODOCUMENTO.TIPODOC "
        Dim qSelect As String = "SELECT MOV_MOVIMIENTO.IDDOCUMENTO, MOV_MOVIMIENTO.NUMERO, MOV_MOVIMIENTO.FECHADOCUMENTO, MOV_MOVIMIENTO.FECHAVENCIMIENTO, MOV_MOVIMIENTO.NOTA, MOV_MOVIMIENTO.NOFACCOMCEL,NVL(MOV_MOVIMIENTO.CERRADO,'N') "
        Dim qOrder As String = "ORDER BY MOV_MOVIMIENTO.NUMERO"
        If cbSedes.SelectedValue <> "00000" Then
            qWhere &= " AND MOV_MOVIMIENTO.IDSEDE='" & cbSedes.SelectedValue & "'"
        End If
        If txtNumero.Text.Length > 0 Then
            qWhere &= " AND MOV_MOVIMIENTO.NUMERO='" & txtNumero.Text & "'"
        End If
        If lTercero.Length > 0 Then
            qWhere &= " AND MOV_MOVIMIENTO.IDTERCERO='" & lTercero & "'"
        End If
        If lSucursal.ToString.Length > 0 AndAlso lSucursal >= 0 Then
            qWhere &= " AND MOV_MOVIMIENTO.SUCURSAL=" & lSucursal & ""
        End If
        If cbDocEs.Text = "Ventas" Or cbDocEs.Text = "Nota Credito" Then
            qFrom &= ", TER_CLIENTES, TER_TERCERO"
            qWhere &= "  AND MOV_MOVIMIENTO.IDTERCERO = TER_CLIENTES.IDCLIENTE(+) AND MOV_MOVIMIENTO.SUCURSAL = TER_CLIENTES.SUCURSAL(+) "
            'qWhere &= " AND MOV_MOVIMIENTO.SALDO>0 "
            qSelect &= " , MOV_MOVIMIENTO.IDTERCERO, TER_CLIENTES.NOMBREESTABLECIMIENTO"


            ENCABEZADOMOVIMIDataGridView.Columns("SUCURSAL").Visible = True
            ENCABEZADOMOVIMIDataGridView.Columns("IDTERCERO").Visible = True
            ENCABEZADOMOVIMIDataGridView.Columns("NOMBRECLIENTE").Visible = True
            If lSaldo = 0 Then
                qWhere &= "  AND MOV_MOVIMIENTO.SALDO <> 0 "
            End If
        ElseIf cbDocEs.Text = "Compras" Then
            qFrom &= ", TER_TERCERO"
            qSelect &= " , MOV_MOVIMIENTO.IDTERCERO, MOV_MOVIMIENTO.SUCURSAL, TER_TERCERO.NOMBRES || ' ' || TER_TERCERO.APELLIDOS AS NOMBREESTABLECIMIENTO "
            qWhere &= "  AND MOV_MOVIMIENTO.IDTERCERO = TER_TERCERO.IDTERCERO(+)  "
            ENCABEZADOMOVIMIDataGridView.Columns("IDTERCERO").Visible = True
            ENCABEZADOMOVIMIDataGridView.Columns("NOMBRECLIENTE").Visible = True
            ENCABEZADOMOVIMIDataGridView.Columns("SUCURSAL").Visible = False
        Else
            ENCABEZADOMOVIMIDataGridView.Columns("SUCURSAL").Visible = False
            ENCABEZADOMOVIMIDataGridView.Columns("IDTERCERO").Visible = False
            ENCABEZADOMOVIMIDataGridView.Columns("NOMBRECLIENTE").Visible = False
        End If
        cmd.CommandText = qSelect + " " + qFrom + " " + qWhere + " " + qOrder
        cmd.Parameters.Add("PDESDEF", OracleDbType.Date).Value = dtDesdeF.Value.Date
        cmd.Parameters.Add("PHASTAF", OracleDbType.Date).Value = dtHastaF.Value.Date
        cmd.Parameters.Add("PDOCES", OracleDbType.Int16).Value = cbDocEs.SelectedValue
        Try
            ConfigColumna()
            conn.Open()
            DsConsultaMovimi.ENCABEZADOMOVIMI.Rows.Clear()
            DsConsultaMovimi.ENCABEZADOMOVIMI.Load(cmd.ExecuteReader)
            conn.Close()
            ENCABEZADOMOVIMIDataGridView.Focus()
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub ConfigColumna()
        If cbDocEs.Text = "Compras" Then
            ENCABEZADOMOVIMIDataGridView.Columns("NOFACCOMCEL").HeaderText = "No. FAC-COMCEL"
        ElseIf cbDocEs.Text = "Entradas" Then
            ENCABEZADOMOVIMIDataGridView.Columns("NOFACCOMCEL").HeaderText = "No. REM-COMCEL"
        Else
            ENCABEZADOMOVIMIDataGridView.Columns("NOFACCOMCEL").Visible = False
        End If
    End Sub
    Private Sub fListaMovimi_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            daTipoDoc = New lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
            For Each SubC As String In lSubClase.Split(",")
                Dim dtTemGlo As lbGlobalcom.dsGlobal.GLO_DATOSTABLASDataTable = daTipoDoc.GetDataByIdGrupoSubclase("td", SubC)
                DsGlobal.GLO_DATOSTABLAS.Merge(dtTemGlo)
            Next
            If lTipoMovimi.Trim.Length > 0 Then
                Me.cbDocEs.Text = lTipoMovimi
                cbDocEs.Enabled = False
            End If
            ConfigColumna()

            Dim iPer As cPeriodos = New cPeriodos
            dtDesdeF.Value = iPer.DESDEF
            daSedes = New lbGlobalcom.dsGlobalTableAdapters.GLO_SEDESTableAdapter
            daSedes.Fill(DsGlobal.GLO_SEDES)
            Dim drSed As lbGlobalcom.dsGlobal.GLO_SEDESRow = DsGlobal.GLO_SEDES.NewRow
            drSed.IDSEDE = "00000"
            drSed.NOMBRE = "Todas"
            DsGlobal.GLO_SEDES.Rows.Add(drSed)
            'cbSedes.SelectedValue = "00000"
            cbSedes.SelectedValue = CustomPrincipal.GetSede
            cbSedes.Enabled = False
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub AsignaMovimi(ByVal Fila As Integer)
        Try
            'And NCABEZADOMOVIMIDataGridView.CurrentRow.Cells("CERRADO").Value <> "N"
            If Fila >= 0 Then
                lTipoDoc = ENCABEZADOMOVIMIDataGridView.CurrentRow.Cells("TIPODOC").Value
                lNumeroDoc = ENCABEZADOMOVIMIDataGridView.CurrentRow.Cells("NUMERO").Value
                Me.DialogResult = Windows.Forms.DialogResult.OK
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub ENCABEZADOMOVIMIDataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles ENCABEZADOMOVIMIDataGridView.CellMouseDoubleClick
        AsignaMovimi(e.RowIndex)
    End Sub


    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub ENCABEZADOMOVIMIDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ENCABEZADOMOVIMIDataGridView.KeyDown
        If e.KeyData = Keys.Enter Then
            AsignaMovimi(ENCABEZADOMOVIMIDataGridView.CurrentRow.Index)
        End If
    End Sub

    
End Class
